package com.dong.thread.juc;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * CyclicBarrier 循环栅栏
 *
 * @author by jiweidong on 2021/11/27.
 */
public class T04_CyclicBarrier {

    public static void main(String[] args) {

//        CyclicBarrier barrier = new CyclicBarrier(20);
        CyclicBarrier barrier = new CyclicBarrier(20, () -> {
            System.out.println("够20个线程了，继续下面执行。。。");
        });

        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
//                System.out.println("barrier before");
                try {
                    barrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
//                System.out.println("barrier after");
            }).start();

        }

    }

}
